	var showCreateEntityMenu = function(){
		
		var attributeNumber = 0;
		var relationNumber = 0;
		var rightMenu = dojo.byId("rightMenu");
 		
		deletePreviusRightMenu();
 		
		var createEntity = dojo.create("div",{ innerHTML: "Create new entity",
			style: "padding-top: 10px; padding-bottom: 10px; font-size: 13px; color: #aaaaaa;",
			align: "center",
 		});
 		rightMenu.appendChild(createEntity);
 		
 		var option = dojo.create("div", {style : "widht: 100%; min-heigth: 50px; background: #333333; padding-left: 10px;",
		});	 		
 		
 		rightMenu.appendChild(option);
 		
 		//{entityName: "", attributes: [{attribute: ""}], relations: [{relatedEntity: "", multiplicity: "", relationName: ""}], objects: [ [{attribute: "", value: "" },{attribute: "", value: "" },] ] }
 		var entiyNameLabel = dojo.create("div", {innerHTML: "Entity name:",
 			style: "padding-top: 10px; padding-bottom: 10px; font-size: 13px; color: #aaaaaa;",
 		});
 		var entityNameInput = dojo.create("input",{id: "entityName"+elementsIdCounter, style: "width: 3em;"});
 		elementsIdCounter++;
 		
 		var attributeContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var attributeTitleContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var attributesLabel = dojo.create("div", {innerHTML: "Attributes:",
 			style: "font-size: 13px; color: #aaaaaa;",
 		});
 		var addAttributeButton = dojo.create("div", {style : "width:18px; height:18px; border: 1px solid #454545; background:#272727; text-align: center; color: #e6e6e6; font-weight: bold; cursor: pointer; float: right; margin-right: 10px;",
			innerHTML : "+",
			onclick : function(){addAttribute(attributeContainer, attributeNumber); attributeNumber++;},
		});
 		attributeTitleContainer.appendChild(addAttributeButton);
 		attributeTitleContainer.appendChild(attributesLabel);
 		
 		var relationsContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var relationsTitleContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var relationLabel = dojo.create("div", {innerHTML: "Relations:",
 			style: "font-size: 13px; color: #aaaaaa;",
 		});
 		var addRelationButton = dojo.create("div", {style : "width:18px; height:18px; border: 1px solid #454545; background:#272727; text-align: center; color: #e6e6e6; font-weight: bold; cursor: pointer; float: right; margin-right: 10px;",
			innerHTML : "+",
			onclick : function(){addRelation(relationsContainer, relationNumber); relationNumber++;},
		});
 		relationsTitleContainer.appendChild(addRelationButton);
 		relationsTitleContainer.appendChild(relationLabel);
 		
 		var buttonContainer = dojo.create("div",{style: "text-align: center;"});
 		var saveButton = dojo.create("input",{type: "button", value: "Save",
 			onclick: function(){saveEntity(entityNameInput, attributeContainer, relationsContainer);},
 		});
 		var cancelButton = dojo.create("input",{type: "button", value: "Cancel",
 			onclick: function(){showCreateEntityMenu();},
 		});
 		buttonContainer.appendChild(saveButton);
 		buttonContainer.appendChild(cancelButton);
 		
 		option.appendChild(entiyNameLabel);
 		option.appendChild(entityNameInput);
 		option.appendChild(attributeTitleContainer);
 		option.appendChild(attributeContainer);
 		option.appendChild(relationsTitleContainer);
 		option.appendChild(relationsContainer);
 		option.appendChild(dojo.create("br",{}));
 		option.appendChild(buttonContainer);
 		
 		new dijit.form.ValidationTextBox({required: "true", promptMessage: "Insert the name of the new entity", style: "width: 180px;"}, entityNameInput.id);
		
	};
	
	var addRelation = function(relationsContainer, relationNumber){
	
		var relatedEntityLabel = dojo.create("div",{innerHTML: "Related entity: ",
			style: "font-size: 13px; color: #aaaaaa; font-style: italic;",	
		});
		var multiplicityLabel = dojo.create("div",{innerHTML: "Multiplicity: ",
			style: "font-size: 13px; color: #aaaaaa; font-style: italic;",	
		});
		
		var	relationNameLabel = dojo.create("div",{innerHTML: "Relation name: ",
			style: "font-size: 13px; color: #aaaaaa; font-style: italic;",	
		});
		
		var relatedEntitySelect = dojo.create("select", {id: "e"+relationNumber,
			style : "float: right; margin-right: 10px;",
		});
		fillRelatedEntity(relatedEntitySelect);		
		
		var relationNameInput = dojo.create("input", {id: "n"+relationNumber,
			style : "float: right; margin-right: 10px;",
		});
		
		var multiplicitySelect = dojo.create("select", {id: "m"+relationNumber,
			style : "float: right; margin-right: 10px;",
		});
		
		var oneMultiplicity = dojo.create("option", {value: "1", innerHTML: "1"});
		var nMultiplicity = dojo.create("option", {value: "n", innerHTML: "n"});
		multiplicitySelect.appendChild(oneMultiplicity);
		multiplicitySelect.appendChild(nMultiplicity);
		
		relationsContainer.appendChild(dojo.create("br",{}));	
		relationsContainer.appendChild(relatedEntitySelect);
		relationsContainer.appendChild(relatedEntityLabel);
		relationsContainer.appendChild(dojo.create("br",{}));
		relationsContainer.appendChild(relationNameInput);
		relationsContainer.appendChild(relationNameLabel);
		relationsContainer.appendChild(dojo.create("br",{}));	
		relationsContainer.appendChild(multiplicitySelect);
		relationsContainer.appendChild(multiplicityLabel);
		relationsContainer.appendChild(dojo.create("br",{}));	
		relationsContainer.appendChild(dojo.create("div",{style: "height: 1px; width: 100%; background: #454545;"}));
	};
	
	var addAttribute = function(attributeContainer, attributeNumber){
		
		var keyLabel = dojo.create("div",{innerHTML: "Name: ",
			style: "font-size: 13px; color: #aaaaaa; font-style: italic;",	
		});
//		var valueLabel = dojo.create("div",{innerHTML: "Value: ",
//			style: "font-size: 13px; color: #aaaaaa; font-style: italic;",	
//		});
		
		var keyInput = dojo.create("input", {id: "k"+attributeNumber,
			style : "float: right; margin-right: 10px;",
		});
//		var valueInput = dojo.create("input", {id: "v"+attributeNumber,
//			style : "float: right; margin-right: 10px;",
//		});
		
		attributeContainer.appendChild(dojo.create("br",{}));	
		attributeContainer.appendChild(keyInput);
		attributeContainer.appendChild(keyLabel);
		attributeContainer.appendChild(dojo.create("br",{}));
//		attributeContainer.appendChild(valueInput);
//		attributeContainer.appendChild(valueLabel);
//		attributeContainer.appendChild(dojo.create("br",{}));	
		attributeContainer.appendChild(dojo.create("div",{style: "height: 1px; width: 100%; background: #454545;"}));
		
	};
	
	var fillRelatedEntity = function(relatedEntitySelect){
		
		for(var i=0; i<dataProvider.length; i++){
			var en = dataProvider[i].entityName;
			relatedEntitySelect.appendChild(dojo.create("option",{innerHTML: en, value: en}));
		}
		
	};
	
	var saveEntity = function(entityNameInput, attributeContainer, relationsContainer){
		
		//{entityName: "", attributes: [{attribute: ""}], relations: [{relatedEntity: "", multiplicity: "", relationName: ""}], objects: [{attribute: "", value: "" }] }
		var newEntity = {entityName: dijit.byId(entityNameInput.id).value, attributes: [], relation: [], objects: []};
		
		var ac = dojo.query(attributeContainer).children();
		var rc = dojo.query(relationsContainer).children();
		
		for(var i=0; i<ac.length; i++){
			if(ac[i].id.indexOf("k")!=-1){
				var key = ac[i].value;
//				var value = dojo.byId( ac[i].id.replace("k","v")).value;
//				var attribute = {key: key, value: value};
				var attribute = {attribute: key};
				newEntity.attributes.push(attribute);
			}
		}
		
		for(var j=0; j<rc.length; j++){
			if(rc[j].id.indexOf("e")!=-1){
				var entityRelated = rc[j].value;
				var nameRelation = dojo.byId(rc[j].id.replace("e", "n")).value;
				var multiplicityRelation = dojo.byId(rc[j].id.replace("e", "m")).value;
				var relation = {relatedEntity: entityRelated, multiplicity: multiplicityRelation, relationName: nameRelation};
				newEntity.relation.push(relation);
			}
		}
		
		dataProvider.push(newEntity);
		printDataProvider();
		showCreateEntityMenu();
	};
	
	var printDataProvider = function(){
		
		for(var i=0; i<dataProvider.length; i++){
			console.log(dataProvider[i].entityName + " " + dataProvider[i].relation.length + " " + dataProvider[i].attributes.length);
		}
	};
	
	/* ************************************** Objects ****************************************** */
	
	var showCreateObjectMenu = function(){

		attributeNumber = 0;
		var rightMenu = dojo.byId("rightMenu");
 		
		deletePreviusRightMenu();
 		
		var createEntity = dojo.create("div",{ innerHTML: "Create new object",
			style: "padding-top: 10px; padding-bottom: 10px; font-size: 13px; color: #aaaaaa;",
			align: "center",
 		});
 		rightMenu.appendChild(createEntity);
 		
 		var option = dojo.create("div", {style : "widht: 100%; min-heigth: 50px; background: #333333; padding-left: 10px;",
		});	 		
 		
 		rightMenu.appendChild(option);
 		
 		//{entityName: "", attributes: [{attribute: ""}], relations: [{relatedEntity: "", multiplicity: "", relationName: ""}], objects: [{attribute: "", value: "" }] }
 		var objectClassLabel = dojo.create("div", {innerHTML: "Select the Class of the new object:",
 			style: "padding-top: 10px; padding-bottom: 10px; font-size: 13px; color: #aaaaaa;",
 		});
 		var objectClassSelect = dojo.create("select",{
 			onchange: function(){fillAttributeValueContainer(attributeValueContainer,this,attributeNumber);},
 		});
 		attributeNumber++;
 		
 		for(var i =0; i<dataProvider.length; i++){
 			var tmp = dojo.create("option", {value: dataProvider[i].entityName, innerHTML: dataProvider[i].entityName});
 			objectClassSelect.appendChild(tmp);
 		}
 		
 		var attributeValueContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var attributeTitleContainer = dojo.create("div",{
 			style: "padding-top: 10px;"
 		});
 		
 		var attributesLabel = dojo.create("div", {innerHTML: "Attributes:",
 			style: "font-size: 13px; color: #aaaaaa;",
 		});
 	
 		attributeTitleContainer.appendChild(attributesLabel);
 		
 		var buttonContainer = dojo.create("div",{style: "text-align: center;"});
 		var saveButton = dojo.create("input",{type: "button", value: "Save",
 			onclick: function(){saveObject(objectClassSelect,attributeValueContainer);},
 		});
 		var cancelButton = dojo.create("input",{type: "button", value: "Cancel",
 			onclick: function(){showCreateObjectMenu();},
 		});
 		buttonContainer.appendChild(saveButton);
 		buttonContainer.appendChild(cancelButton);
 		
 		option.appendChild(objectClassSelect);
 		option.appendChild(objectClassLabel);
 		
 		option.appendChild(attributeTitleContainer);
 		option.appendChild(attributeValueContainer);
 		
 		option.appendChild(buttonContainer);
 		
	};
	
	var fillAttributeValueContainer = function(attributeValueContainer,objectClassSelect,attributeNumber){
		
		dojo.empty(attributeValueContainer);
		
		for(var i=0; i<dataProvider.length; i++){
			if(dataProvider[i].entityName == objectClassSelect.value){
				for(var j=0; j<dataProvider[i].attributes.length; j++){
					var label = dojo.create("div",{
						innerHTML: dataProvider[i].attributes[j].attribute+":",
						style: "padding-top: 10px; padding-bottom: 5px; font-size: 13px; color: #aaaaaa;",
					});
					var input = dojo.create("input", {id: dataProvider[i].attributes[j].attribute,
						style: "padding-bottom: 10px;",
					});
					attributeValueContainer.appendChild(label);
					attributeValueContainer.appendChild(input);
				}
			}
		}
	};
	
	var saveObject = function(objectClassSelect,attributeValueContainer){
		
		var index;
		var attributes = [];
		for(var i=0; i< dataProvider.length; i++){
			if(dataProvider[i].entityName == objectClassSelect.value){
				index = i;
				break;
			}
		}
		
		var avc = dojo.query(attributeValueContainer).children();
		
		//{entityName: "", attributes: [{attribute: ""}], relations: [{relatedEntity: "", multiplicity: "", relationName: ""}], objects: [ [{attribute: "", value: "" },{attribute: "", value: "" },] ] }
		for(var j=0; j<avc.length; j++){
			
			if(avc[j].id != ""){
				var attrName = avc[j].id;
				var attrVal = avc[j].value;
				var attributeObj = {attribute: attrName, value: attrVal};
				attributes.push(attributeObj);
			}
		}
		
		dataProvider[index].objects.push(attributes);
		printObject();
		showCreateObjectMenu();
		
	};
	
	var printObject = function(){
		console.log("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
		for(var i=0; i<dataProvider.length; i++){
			for(var j=0; j<dataProvider[i].objects.length; j++){
				for(var v=0; v<dataProvider[i].objects[j].length; v++){
					console.log(dataProvider[i].objects[j][v].attribute + ": " + dataProvider[i].objects[j][v].value);
				}
			}
		}
	};